﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using UT3Bots.UTItems;
using UT3Bots.Communications;

namespace UT3Bots.Visualizer.Controls
{
    public partial class UTUIBot : UserControl
    {
        private UTBotOppState _bot;
		private bool _isChatting = false;



		public UTUIBot(UTBotOppState bot)
		{
			this._bot = bot;
			this.DataContext = this._bot;
			InitializeComponent();

			this._bot.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Bot_PropertyChanged);

			stbChatBubbleIn.Completed += new EventHandler(stbChatBubbleIn_Completed);

			//Set Name
			this.txtName.Text = _bot.Name;
			this.txtNameShadow.Text = _bot.Name;
			Canvas.SetLeft(this.txtName, this.txtName.ActualWidth * -0.5);
			Canvas.SetLeft(this.txtNameShadow, (this.txtName.ActualWidth * -0.5) + 1);

			//Set Bot Color
			switch (_bot.Color)
			{
				case BotColor.Red:
					this.botCircle.Fill = new RadialGradientBrush(Color.FromArgb(255, 255, 100,100), Colors.Red);
					break;

				case BotColor.Blue:
					this.botCircle.Fill = new RadialGradientBrush(Colors.White, Colors.Blue);
					break;

				case BotColor.None:
					this.botCircle.Fill = new RadialGradientBrush(Colors.White, Colors.Gray);
					break;

				case BotColor.Random:
					this.botCircle.Fill = new RadialGradientBrush(Colors.Yellow, Colors.Green);
					break;
			}

		}

		private void Bot_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			switch (e.PropertyName)
			{
				case "LastChatMessage":
					Chat(this._bot.LastChatMessage);
					break;
				case "Health":
					botHealth.Height = botHealthContainer.Height * (this._bot.Health / 200d);
					break;
				case "RotationAngle":
					transRotate.Angle = this._bot.RotationAngle - 180;
					break;
			}
		}

		private void stbChatBubbleIn_Completed(object sender, EventArgs e)
		{
			_isChatting = false;
		}

		public void Chat(string message)
		{
			txtChat.Text = message;
			if (txtChat.ActualHeight > botChatBubble.Height - 10)
			{
				txtChat.VerticalAlignment = VerticalAlignment.Top;
			}
			else
			{
				txtChat.VerticalAlignment = VerticalAlignment.Center;
			}

			//if (_isChatting == false)
			{
				_isChatting = true;
				stbChatBubbleIn.Begin();
			}
		}
	}
}
